﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PublicTemplateEditor.Helper;
using PublicTemplateEditor.Mold;
using System.Windows.Controls;

namespace PublicTemplateEditor.DataAccess
{
    public class ElementDA
    {

        #region 保存元素操作
        //插入元素sql
        private static string InsertElementSQL = "insert into m_element (id,pid,ename,Ecfg) values('{0}','{1}','{2}','{3}') ; ";
 
        //插入元素sql
        public static string InsertEditObjectSql(EditObject eo, string pageID)
        {
            string sql = string.Empty;
            sql = string.Format(InsertElementSQL,eo.Guid,pageID, eo.Name,eo.ConfigStr);
            return sql;
        }

        //插入元素sql
        public static string InsertEditObjectSql(EditObjectDB eo)
        {
            string sql = string.Empty;
            sql = string.Format(InsertElementSQL, eo.ID, eo.PID, eo.Name, eo.ConfigStr);
            return sql;
        }

        //更新元素sql
        public static string UpdateEditObjectSql(EditObject eo)
        {
            string sql = string.Empty;
            string UpdateElementSQL = "update m_element set ename='{0}',ecfg='{1}',modify=datetime('now','localtime') where id='{2}'; ";
            sql = string.Format(UpdateElementSQL, eo.Name, eo.ConfigStr,eo.Guid);
            return sql;
        }

        //删除元素
        public static string DeleteEditObjectSql(string str)
        {
            string sql = string.Empty;
            string DeleteElementSQL = " delete from m_element where  id in({0});";
            sql = string.Format(DeleteElementSQL, str);
            return sql;
        }


        #endregion

        #region 读取元素操作

        /// <summary>
        /// 把数据库中读取的数据解析出来
        /// </summary>
        /// <param name="source">元素配置</param>
        /// <param name="tb">信息提示框</param>
        /// <returns></returns>
        public static EditObjectList GetEditObject(List<EditObjectDB> source)
        {
            #region 定义
            //输出的元素集合
            EditObjectList newEditObjectList = new EditObjectList();
            //输出的元素，临时
            EditObject newEditObject = null;
            #endregion

            //StringBuilder sb = new StringBuilder();
            //DateTime dt1;
            //TimeSpan ts;
            for (int j = 0; j < source.Count(); j++)
            {
                //dt1 = DateTime.Now;
                //newEditObject = new EditObject(source[j].ConfigStr);
                newEditObject = EditObject.analysis(source[j].ConfigStr);
                if (newEditObject == null)
                    continue;
                newEditObject.Guid = source[j].ID;
                newEditObject.IsOperate = false;
                newEditObject.Name = newEditObjectList.GetNewName(newEditObject.EditType);
                newEditObjectList.Add(newEditObject);
                //ts = DateTime.Now - dt1;
                //sb.Append(j.ToString() + "\r\n配置为" + source[j].ConfigStr + "\r\n开始时间:" + dt1.ToString("HH:mm:ss fff") + "\r\n结束时间:" + DateTime.Now.ToString("HH:mm:ss fff") + "\r\n耗时:" + ts.TotalMilliseconds.ToString() + "\r\n");
            }

            //FileHelper.WriteTestFile(sb.ToString());
                return newEditObjectList;
        }
        #endregion

 
    }
}
